package com.hllxd.hism.star.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.io.IOException;

@ControllerAdvice
@Slf4j
public class DefaultExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    //@ResponseBody
    public Object conversionErrorHandler(Exception exception) {
        log.warn("Error : {}", exception.toString(), exception);
        if (exception instanceof MissingServletRequestParameterException || exception instanceof HttpRequestMethodNotSupportedException) {
            return new Result("00000101", exception.getMessage(), false, "");
        }
        if (exception instanceof IOException || exception instanceof NullPointerException) {
            return new Result("00000102", exception.getMessage(), false, "");
        }
        if (exception instanceof BusinessException) {
            return new Result(((BusinessException) exception).getCode(), ((BusinessException) exception).getMsg(), false, "");
        }
        return new Result("00000104", exception.getMessage(), false, "");
    }
}
